{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.5 幂级数(p272)\n",
    "\n",
    "使用 symPy.Sum 求解级数\n",
    "\n",
    "Sum 函数使用方法：Sum(func, (variables, start, end))，第一个参数是待求级数通项，第二个元组形式参数接受变量以及级数的起始与终止数值。\n",
    "\n",
    "注意事项：使用 Sum 求和，不会自动对极限、积分、求和以及乘积进行计算，它仅是创建一个和式。若要完成相关计算操作（如果可以的话），可用 Sum.doit()；若希望计算数值，使用 Sum.evalf()。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.5.1 幂级数求和\n",
    "\n",
    "例：求 S(x)=\\sum_{n=1}^{\\infty} \\frac{n^3}{(n + 1)}x^n 的和函数。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "((x**3 + x - 1)*exp(x) + 1)/x\n"
     ]
    }
   ],
   "source": [
    "import sympy\n",
    "\n",
    "# 定义符号变量 n 和 x\n",
    "n = sympy.Symbol('n')\n",
    "x = sympy.Symbol('x')\n",
    "\n",
    "# 定义函数 f\n",
    "f = sympy.Lambda(x, (n**3 / sympy.factorial(n + 1)) * x**n)\n",
    "# 使用 Sum 进行求和，并尝试计算结果（这里要注意调用 doit 和 simplify 的顺序可以根据需求调整）\n",
    "result = sympy.Sum(f(x), (n, 0, sympy.oo)).doit().simplify()\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.5.2 幂级数展开\n",
    "\n",
    "使用 series () 函数展开为幂级数：\n",
    "\n",
    "使用方法：series(func, x, x0, n)，其中 func 为待展开函数，x 为目标展开变量（不设置程序自动判别），x0 为展开点，n 为展开阶数（默认 n = 6，即展开到 6 阶为止）。\n",
    "\n",
    "例：将函数 f(x)=\\frac{x}{9 + x^2} 展开成 x 的幂级数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Lambda(_x, _x)\n"
     ]
    }
   ],
   "source": [
    "import sympy\n",
    "\n",
    "# 定义符号变量 x\n",
    "x = sympy.Symbol('x')\n",
    "\n",
    "# 定义函数 f\n",
    "f = sympy.Lambda(x, x)\n",
    "\n",
    "# 正确调用 fourier_series 函数，传入函数 f 本身以及对应的区间参数\n",
    "result = sympy.fourier_series(f, (x, -1, 1))\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "使用 fourier_series () 函数展开为傅里叶级数：\n",
    "\n",
    "使用方法：fourier_series(func, (var, start, end))，func 为待展开函数，var 为目标展开变量，start 为展开区间左侧，end 为展开区间右侧。\n",
    "\n",
    "例：设函数周期为 1，在 [0, 1] 的表达式为 f(t)=t(0≤t<1)，将其展开成傅立叶级数。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Lambda(_x, _x)\n"
     ]
    }
   ],
   "source": [
    "import sympy\n",
    "\n",
    "# 定义符号变量x\n",
    "x = sympy.Symbol('x')\n",
    "\n",
    "# 正确定义函数f，使用sympy.Lambda来定义匿名函数\n",
    "f = sympy.Lambda(x, x)\n",
    "\n",
    "# 调用fourier_series函数，传入函数f以及对应的区间参数\n",
    "result = sympy.fourier_series(f, (x, -1, 1))\n",
    "print(result)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "4.5.3 幂级数计算\n",
    "\n",
    "例：使用麦克劳林展开式求前 5、10、20 项数值，近似计算。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "7.0 7.388712522045855 7.389056098930174\n"
     ]
    }
   ],
   "source": [
    "import sympy\n",
    "from sympy import Function\n",
    "\n",
    "# 定义符号变量x\n",
    "x = sympy.Symbol('x')\n",
    "\n",
    "# 创建函数对象f\n",
    "f = Function('f')\n",
    "f5 = sympy.Lambda(x, sympy.series(sympy.exp(x), x, 0, 5)).subs(sympy.O(x**5), 0)\n",
    "f10 = sympy.Lambda(x, sympy.series(sympy.exp(x), x, 0, 10)).subs(sympy.O(x**10), 0)\n",
    "f20 = sympy.Lambda(x, sympy.series(sympy.exp(x), x, 0, 20)).subs(sympy.O(x**20), 0)\n",
    "\n",
    "print(float(f5(2)), float(f10(2)), float(f20(2)))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
